package cn.bdqn.sptparent.web.controller;


import cn.bdqn.sptparent.common.util.RedisUtil;
import cn.bdqn.sptparent.domain.Result;
import cn.bdqn.sptparent.domain.param.LoginParam;
import cn.bdqn.sptparent.domain.po.Product;
import cn.bdqn.sptparent.service.CategoryService;
import cn.bdqn.sptparent.service.ProductService;
import cn.bdqn.sptparent.web.mq.MQMessage;
import cn.bdqn.sptparent.web.mq.MQSender;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lhl
 * @since 2023-05-17
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Resource
    private ProductService productService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private MQSender mqSender;

    @GetMapping("view/{id}")
    public String view(@PathVariable("id") Integer productId, Model model){
        String key = "product:view:"+productId;
        if(redisUtil.hasKey(key)){// 缓存存在
            model.addAttribute("product",JSON.parse(redisUtil.get(key).toString()));
            model.addAttribute("categoryList",categoryService.list());
        }else{
            Product product = productService.getById(productId);
            redisUtil.set(key,product,60);
            model.addAttribute("product",product);
            model.addAttribute("categoryList",categoryService.list());
        }
        return "product/index";
    }
    @GetMapping("/{id}")
    @ResponseBody
    public Result get(@PathVariable("id") Integer productId, Model model){
        String key = "product:view:"+productId;
        if(redisUtil.hasKey(key)){// 缓存存在
            return Result.ok().data(JSON.parse(redisUtil.get(key).toString()));
        }else{
            Product product = productService.getById(productId);
            redisUtil.set(key,product,60);
            return Result.ok().data(product);
        }
    }


    // 页面静态化
    @Resource
    private ThymeleafViewResolver thymeleafViewResolver;
    @RequestMapping(value = "view2/{id}",produces = "text/html;charset=UTF-8")// 响应格式为Html
    @ResponseBody
    public String address(@PathVariable("id") Integer productId,HttpServletRequest request, HttpServletResponse response,Model model) {
        String key = "product:view2:" + productId;
        // 获取缓存
        Object obj = redisUtil.get(key);
        String html = null;
        if(obj != null){
            html = obj.toString();
        }
        try {
            if (html != null && !"".equals(html)) {
                return html;
            }
            // model参数
            Product product = productService.getById(productId);
            model.addAttribute("product", product);
            model.addAttribute("categoryList", categoryService.list());
            // 页面渲染
            WebContext wct = new WebContext(request, response, request.getServletContext(),
                    request.getLocale(), model.asMap());
            html = thymeleafViewResolver.getTemplateEngine().process("product/index.html", wct);
            // 异步存储redis
            mqSender.sendHTML(new MQMessage<String>(key,html));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return html;
    }
}
